﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.Modules;
using WP.Controls;
using WP.Controls.GridColumn;


using WP.RM.BusinessRule;
using WP.RM.BusinessLogical;
using WP.RM.DataAccess;

namespace WP.RM.Form
{
    public partial class frmRMWKFormula : BaseQryRpt 
    {
        private DBAccess mdbcon = new DBAccess(false, false);
        private bool[] mDtlColumnReadOnlyArray;
        private string mstrBOMRecID = "";
        public  enum FormulaType
        {
            FromCategorySetBOM =0,
            FromProductBOM,
            PriceCategorySetBOM,
            PriceProductBOM,
            OtherCategorySetBOM,
            OtherProductBOM,
            FromProductBOMRev,
            OtherProductBOMRev,
            FromProductBOMSales,
            OtherProductBOMSales,
            SemiProductBOM
        }

        private enum DtlCol
        {
            SeqNo=0,           
            Type,
            CHNName ,    //对应的中文名称
            Description, //描述
            FieldName,   //对应的字段名称
            TableName,   //对应的字段所在的表明
            RecID,
        }

        private FormulaType mFormulaType = new FormulaType();        

        public frmRMWKFormula( )
        {
            InitializeComponent();
            LoadAllControl(new ArrayList(this.Controls));
            this.MinimumSize = this.Size;
            mFormulaType = FormulaType.FromCategorySetBOM;
            InitGrid();
            InitGridItem();
            FillGrid();
        }

        public frmRMWKFormula( string strBOMRecID , FormulaType formulaType )
        {
            InitializeComponent();
            LoadAllControl(new ArrayList(this.Controls));
            this.MinimumSize = this.Size;
            mstrBOMRecID = strBOMRecID;
            mFormulaType = formulaType;
            InitGrid();
            InitGridItem();
            FillGrid();
            FillFormula();
        }
        
        private void InitGrid()
        {
            this.dgvCalInfo.Columns.Clear();
            this.dgvCalInfo.Rows.Clear();

            DataGridViewTextBoxColumn colSeqNo = new DataGridViewTextBoxColumn();
            colSeqNo.HeaderText = "序号";
            colSeqNo.Visible = false;
            colSeqNo.Name = "SeqNo";
            colSeqNo.Width = 30;
            colSeqNo.ReadOnly = true;

            DataGridViewTextBoxColumn colType = new DataGridViewTextBoxColumn();
            colType.HeaderText = "类别";
            colType.Width = 50;
            colType.Name = "";
            colType.ReadOnly = false;
            colType.Visible = false;

            DataGridViewTextBoxColumn colWKName = new DataGridViewTextBoxColumn();
            colWKName.HeaderText = "名称";
            colWKName.Width = 150;
            colWKName.Name = "";
            colWKName.ReadOnly = false;

            DataGridViewTextBoxColumn colDescription = new DataGridViewTextBoxColumn();
            colDescription.HeaderText = "描述";
            colDescription.Width = 150;
            colDescription.Name = "";
            colDescription.ReadOnly = false;

            DataGridViewTextBoxColumn colFieldName = new DataGridViewTextBoxColumn();
            colFieldName.HeaderText = "字段";
            colFieldName.Width = 0;
            colFieldName.Name = "FieldName";
            colFieldName.ReadOnly = true ;
            colFieldName.Visible = false;

            DataGridViewListColumn colTableName = new DataGridViewListColumn();
            colTableName.HeaderText = "表名";
            colTableName.Width = 80;
            colTableName.Name = "TableName";
            colTableName.ReadOnly = false;
            colTableName.Visible = false;

            DataGridViewTextBoxColumn colRecID = new DataGridViewTextBoxColumn();
            colRecID.HeaderText = "RecID";
            colRecID.Visible = false;
            colRecID.Name = "RecID";

            this.dgvCalInfo.Columns.AddRange(new DataGridViewColumn[] { colSeqNo,colType,colWKName, colDescription  ,colFieldName,colTableName,colRecID});

            mDtlColumnReadOnlyArray = new bool[dgvCalInfo.Columns.Count];
            string[] ColumnWidthArray = DataFunctions.GetGridColumnWidthString(this.Name, this.CurrentMenuInfo.iParameter, SysVar.UserName, this.Name + "." + this.dgvCalInfo.Name).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < dgvCalInfo.Columns.Count; i++)
            {
                mDtlColumnReadOnlyArray[i] = dgvCalInfo.Columns[i].ReadOnly;
                if (ColumnWidthArray.Length == dgvCalInfo.Columns.Count) dgvCalInfo.Columns[i].Width = CvtFunc.ToInt(ColumnWidthArray[i]);
            }

            this.dgvCalInfo.EditMode = DataGridViewEditMode.EditOnEnter;
            this.dgvCalInfo.RowTemplate.Height = 20;
            this.dgvCalInfo.RowHeadersWidth = 23;
            this.dgvCalInfo.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
            this.dgvCalInfo.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            this.dgvCalInfo.ColumnHeadersHeight = 30;
            this.dgvCalInfo.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.dgvCalInfo.SelectionMode = DataGridViewSelectionMode.CellSelect;
            this.dgvCalInfo.AllowUserToResizeRows = false;
            
        }
        private void InitGridItem()
        {
            this.dgvItem.Columns.Clear();
            this.dgvItem.Rows.Clear();

            DataGridViewTextBoxColumn colSeqNo = new DataGridViewTextBoxColumn();
            colSeqNo.HeaderText = "序号";
            colSeqNo.Visible = false;
            colSeqNo.Name = "SeqNo";
            colSeqNo.Width = 30;
            colSeqNo.ReadOnly = true;

            DataGridViewTextBoxColumn colType = new DataGridViewTextBoxColumn();
            colType.HeaderText = "类别";
            colType.Width = 50;
            colType.Name = "";
            colType.ReadOnly = false;
            colType.Visible = false;

            DataGridViewTextBoxColumn colWKName = new DataGridViewTextBoxColumn();
            colWKName.HeaderText = "名称";
            colWKName.Width = 150;
            colWKName.Name = "";
            colWKName.ReadOnly = false;

            DataGridViewTextBoxColumn colDescription = new DataGridViewTextBoxColumn();
            colDescription.HeaderText = "描述";
            colDescription.Width = 150;
            colDescription.Name = "";
            colDescription.ReadOnly = false;

            DataGridViewTextBoxColumn colFieldName = new DataGridViewTextBoxColumn();
            colFieldName.HeaderText = "字段";
            colFieldName.Width = 0;
            colFieldName.Name = "FieldName";
            colFieldName.ReadOnly = true;
            colFieldName.Visible = false;

            DataGridViewListColumn colTableName = new DataGridViewListColumn();
            colTableName.HeaderText = "表名";
            colTableName.Width = 80;
            colTableName.Name = "TableName";
            colTableName.ReadOnly = false;
            colTableName.Visible = false;

            DataGridViewTextBoxColumn colRecID = new DataGridViewTextBoxColumn();
            colRecID.HeaderText = "RecID";
            colRecID.Visible = false;
            colRecID.Name = "RecID";

            this.dgvItem.Columns.AddRange(new DataGridViewColumn[] { colSeqNo, colType, colWKName, colDescription, colFieldName, colTableName, colRecID });

            mDtlColumnReadOnlyArray = new bool[dgvCalInfo.Columns.Count];
            string[] ColumnWidthArray = DataFunctions.GetGridColumnWidthString(this.Name, this.CurrentMenuInfo.iParameter, SysVar.UserName, this.Name + "." + this.dgvItem.Name).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < dgvCalInfo.Columns.Count; i++)
            {
                mDtlColumnReadOnlyArray[i] = dgvCalInfo.Columns[i].ReadOnly;
                if (ColumnWidthArray.Length == dgvCalInfo.Columns.Count) dgvItem.Columns[i].Width = CvtFunc.ToInt(ColumnWidthArray[i]);
            }

            this.dgvItem.EditMode = DataGridViewEditMode.EditOnEnter;
            this.dgvItem.RowTemplate.Height = 20;
            this.dgvItem.RowHeadersWidth = 23;
            this.dgvItem.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
            this.dgvItem.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            this.dgvItem.ColumnHeadersHeight = 30;
            this.dgvItem.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.dgvItem.SelectionMode = DataGridViewSelectionMode.CellSelect;
            this.dgvItem.AllowUserToResizeRows = false;
            FillGrid();
        }
        private void FillGrid()
        {
            FillGrid("");
        }

        private string GetTableName()
        {           
            string strTableName = "";
            if (mFormulaType == FormulaType.FromCategorySetBOM || mFormulaType == FormulaType.OtherCategorySetBOM || mFormulaType == FormulaType.PriceCategorySetBOM)
            {
                strTableName = "rmCategorySetBOMHdr";
            }           
            else if (mFormulaType == FormulaType.FromProductBOM || mFormulaType == FormulaType.PriceProductBOM || mFormulaType == FormulaType.OtherProductBOM)
            {
                strTableName = "rmProduct";
            }
            else if (mFormulaType == FormulaType.FromProductBOMRev || mFormulaType ==  FormulaType.OtherProductBOMRev )
            {
                strTableName = "rmProductBOMRev";
            }
            else if (mFormulaType == FormulaType.FromProductBOMSales || mFormulaType == FormulaType.OtherProductBOMSales)
            {
                strTableName = "rmProductBOMSales";
            }
            else if (mFormulaType == FormulaType.SemiProductBOM)
            {
                strTableName = "rmProduct";
            }
            return strTableName;
        }

        private void FillFormula()
        {
            string strSQL = "";
            if (mFormulaType == FormulaType.FromCategorySetBOM)
            {
                strSQL = "select * from rmWKFormula where BOMRecID='" + mstrBOMRecID + "'";
            }
            else if (mFormulaType == FormulaType.PriceCategorySetBOM)
            {
                strSQL ="select * from rmWKPriceFormula where BOMRecID='"+ mstrBOMRecID +"'";
            }
            else if (mFormulaType == FormulaType.FromProductBOM)
            {
                strSQL = "select * from rmWKProductFormula where BOMRecID='" + mstrBOMRecID + "'";
            }
            else if (mFormulaType == FormulaType.SemiProductBOM)
            {
                strSQL = "select * from rmWKSemiProductFormula where BOMRecID='" + mstrBOMRecID + "'";
            }
            else if (mFormulaType == FormulaType.FromProductBOMRev)
            {
                strSQL = "select * from rmWKProductFormulaRev where BOMRecID='" + mstrBOMRecID + "'";
            }
            else if (mFormulaType == FormulaType.FromProductBOMSales)
            {
                strSQL = "select * from rmWKProductFormulaSales where BOMRecID='" + mstrBOMRecID + "'";
            }
            else if (mFormulaType == FormulaType.PriceProductBOM)
            {
                strSQL = "select * from rmWKProductPriceFormula where BOMRecID='" + mstrBOMRecID + "'";
            }
            else if (mFormulaType == FormulaType.OtherCategorySetBOM)
            {
                strSQL = "select * from rmWKFormulaOtherCategorySetBOM where BOMRecID='" + mstrBOMRecID + "'";
            }
            else if (mFormulaType == FormulaType.OtherProductBOM)
            {
                strSQL = "select * from rmWKFormulaOtherProductBOM where BOMRecID='" + mstrBOMRecID + "'";
            }
            else if (mFormulaType == FormulaType.OtherProductBOMRev)
            {
                strSQL = "select * from rmWKFormulaOtherProductBOMRev where BOMRecID='" + mstrBOMRecID + "'";
            }
            else if (mFormulaType == FormulaType.OtherProductBOMSales)
            {
                strSQL = "select * from rmWKFormulaOtherProductBOMSales where BOMRecID='" + mstrBOMRecID + "'";
            }

            DataRow drFormula = mdbcon.GetDataFirstRow(strSQL);
            if (drFormula != null)
            {
                txtFormula.Text = CvtFunc.ToString(drFormula["WKFormula"]);
            }
            txtFormula.SelectionStart = txtFormula.Text.Length;
        }

        private void FillGrid(string strType)
        {
            this.dgvItem.Rows.Clear();
            this.dgvCalInfo.Rows.Clear();
            string strSQL = "";
            if (strType == "")
            {
                strSQL = "select * from rmWKCalDict where SYSTableName='rmProduct' order by Type,SeqNo, CHNFieldName";
            }
            else
            {
                strSQL = "select * from rmWKCalDict where Type='" + strType + "' and  SYSTableName='rmProduct' order by Type,SeqNo, CHNFieldName";
            }
            DataTable dtFormula = mdbcon.GetDataTable ( strSQL );
            foreach (DataRow drRow in dtFormula.Rows)
            {
                this.dgvCalInfo.Rows.Add(); ;
                this.dgvCalInfo.Rows[this.dgvCalInfo.Rows.Count - 1].Cells[(int)DtlCol.CHNName].Value = CvtFunc.ToString(drRow["CHNFieldName"]);
                this.dgvCalInfo.Rows[this.dgvCalInfo.Rows.Count - 1].Cells[(int)DtlCol.Description].Value = CvtFunc.ToString(drRow["Description"]);
                this.dgvCalInfo.Rows[this.dgvCalInfo.Rows.Count - 1].Cells[(int)DtlCol.FieldName].Value = CvtFunc.ToString(drRow["SysFieldName"]);
                this.dgvCalInfo.Rows[this.dgvCalInfo.Rows.Count - 1].Cells[(int)DtlCol.TableName].Value = CvtFunc.ToString(drRow["SysTableName"]);
                this.dgvCalInfo.Rows[this.dgvCalInfo.Rows.Count - 1].Cells[(int)DtlCol.Type].Value = CvtFunc.ToString(drRow["Type"]);
            }
            this.dgvCalInfo.EndEdit();

            strSQL = "";
            if (mFormulaType != FormulaType.SemiProductBOM)
            {
                if (strType == "")
                {
                    strSQL = "select * from rmWKCalDict where (SYSTableName='iminvmaster' or SYSTableName='ImCategoryWKRating') order by Type,SeqNo, CHNFieldName";
                }
                else
                {
                    strSQL = "select * from rmWKCalDict where Type='" + strType + "' and  SYSTableName='iminvmaster' order by Type,SeqNo, CHNFieldName";
                }
                dtFormula = mdbcon.GetDataTable(strSQL);
                foreach (DataRow drRow in dtFormula.Rows)
                {
                    this.dgvItem.Rows.Add(); ;
                    this.dgvItem.Rows[this.dgvItem.Rows.Count - 1].Cells[(int)DtlCol.CHNName].Value = CvtFunc.ToString(drRow["CHNFieldName"]);
                    this.dgvItem.Rows[this.dgvItem.Rows.Count - 1].Cells[(int)DtlCol.Description].Value = CvtFunc.ToString(drRow["Description"]);
                    this.dgvItem.Rows[this.dgvItem.Rows.Count - 1].Cells[(int)DtlCol.FieldName].Value = CvtFunc.ToString(drRow["SysFieldName"]);
                    this.dgvItem.Rows[this.dgvItem.Rows.Count - 1].Cells[(int)DtlCol.TableName].Value = CvtFunc.ToString(drRow["SysTableName"]);
                    this.dgvItem.Rows[this.dgvItem.Rows.Count - 1].Cells[(int)DtlCol.Type].Value = CvtFunc.ToString(drRow["Type"]);
                }
                this.dgvCalInfo.EndEdit();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            base.OnHelp(); 
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (UpdateData()) this.Close();
        }

        private bool VaildCheck()
        {
            //用来检查表达式
            string strWKFormula = txtFormula.Text;
            if (strWKFormula == "") return true;
            string strSQL = "select * from rmWKCalDict";
            DataTable dtDict = mdbcon.GetDataTable(strSQL);
            foreach (DataRow drDict in dtDict.Rows)
            {
                strWKFormula = strWKFormula.Replace(CvtFunc.ToString(drDict["CHNFieldName"]), CvtFunc.ToString(drDict["SYSTableName"]) + "." + CvtFunc.ToString(drDict["SYSFieldName"]));
            }

            if (strWKFormula.IndexOf ("ImCategoryWKRating.RatingQty", 0, StringComparison.OrdinalIgnoreCase) >= 0)
            {
                strWKFormula = strWKFormula.Replace("ImCategoryWKRating.RatingQty","100");
            }

            if (strWKFormula.IndexOf("iminvmaster", 0, StringComparison.OrdinalIgnoreCase) < 0)
            {
                string strTableName = GetTableName();
                if (strTableName == "") strTableName = "rmProduct";
                strWKFormula = strWKFormula.Replace("rmProduct", strTableName); 
                strSQL = @" select top 1 {0} from {1}";                          
                strSQL = string.Format(strSQL, strWKFormula,strTableName);
            }
            else
            {
                string strTableName = GetTableName();
                if (strTableName == "") strTableName = "rmProduct";
                strWKFormula = strWKFormula.Replace("rmProduct", strTableName); 
                strSQL = @" select top 1 {0} from {1}
                          ,iminvmaster ";
                strSQL = string.Format(strSQL, strWKFormula,strTableName);
            }
            try
            {
                DataRow drQty = mdbcon.GetDataFirstRow(strSQL);
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.ToString());
                if (MessageBox.Show("表达示可能存在错误，请检查后重新输入。是否保存？", SysVar.WarnTitle, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return true;
        }

        private bool UpdateData()
        {
            if (CvtFunc.ToDouble(mstrBOMRecID) < 0) return false;
            if (!VaildCheck()) return false;
            string strTableName = "";
            if (mFormulaType == FormulaType.FromCategorySetBOM)
            {
                strTableName = "rmWKFormula";
            }
            else if ( mFormulaType == FormulaType.FromProductBOM ) 
            {
                strTableName = "rmWKProductFormula";
            }
            else if (mFormulaType == FormulaType.SemiProductBOM)
            {
                strTableName = "rmWKSemiProductFormula";
            }
            else if (mFormulaType == FormulaType.FromProductBOMRev)
            {
                strTableName = "rmWKProductFormulaRev";
            }
            else if (mFormulaType == FormulaType.FromProductBOMSales)
            {
                strTableName = "rmWKProductFormulaSales";
            }
            else if (mFormulaType == FormulaType.PriceCategorySetBOM)
            {
                strTableName = "rmWKPriceFormula";
            }
            else if (mFormulaType == FormulaType.PriceProductBOM)
            {
                strTableName = "rmWKProductPriceFormula";
            }
            else if (mFormulaType == FormulaType.OtherCategorySetBOM)
            {
                strTableName = "rmWKFormulaOtherCategorySetBOM";
            }
            else if (mFormulaType == FormulaType.OtherProductBOM)
            {
                strTableName = "rmWKFormulaOtherProductBOM";
            }
            else if (mFormulaType == FormulaType.OtherProductBOMRev)
            {
                strTableName = "rmWKFormulaOtherProductBOMRev";
            }
            else if (mFormulaType == FormulaType.OtherProductBOMSales)
            {
                strTableName = "rmWKFormulaOtherProductBOMSales";
            }
            string strSQL = "select * from "+ strTableName +" where BOMRecID='"+ mstrBOMRecID +"'";
            DataRow drBOM = mdbcon.GetDataFirstRow(strSQL);
            if ( drBOM ==null )
            {
                strSQL = @" Insert into {2} ( WKFormula, BOMRecID) 
                            VALUES ('{0}','{1}')";
                strSQL = string.Format(strSQL , txtFormula.Text, mstrBOMRecID,strTableName );
            }
            else
            {
                strSQL = "update {2} set WKFormula='{0}' where BOMRecID='{1}'";
                strSQL = string.Format(strSQL, txtFormula.Text, mstrBOMRecID,strTableName);
            }
            mdbcon.ExecuteSql(strSQL);
            return true;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            txtFormula.Text = "";
        }

        private void dgvCalInfo_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex < 0) return;
            if (this.dgvCalInfo.CurrentRow == null) return;
            if ( CvtFunc.ToString ( this.dgvCalInfo.Rows[e.RowIndex].Cells[(int)DtlCol.CHNName].Value) != "")
            {
                string strSelCHNName = CvtFunc.ToString(this.dgvCalInfo.Rows[e.RowIndex].Cells[(int)DtlCol.CHNName].Value);
                string strBefore = txtFormula.Text.Substring(0, txtFormula.SelectionStart);
                string strAfter = txtFormula.Text .Substring ( txtFormula.SelectionStart , txtFormula.Text.Length - txtFormula.SelectionStart - txtFormula.SelectionLength  );
                txtFormula.Text = strBefore + strSelCHNName + strAfter ;
            }
            txtFormula.SelectionStart = txtFormula.Text.Length;
        }

        protected override void ubtLookUp_Click(object sender, EventArgs e)
        {
            string strSql = string.Empty;
            //Control ubt = sender as Control;
            ButtonEx ubt = sender as ButtonEx;
            bool blnHasSetFocus = false;
            Control con = null;

            string strCaption = string.Empty;
            string strFindValue = string.Empty;
            string strLookUpField = string.Empty;

            switch (ubt.Name)
            {
                case "ubtFormula":

                    strSql = "select WKName as 工式名称, Formula as 计算工式, Remark as 备注 from rmFormula";
                    strCaption = "计算公式";
                    strFindValue = txtFormula.Text  ;
                    strLookUpField = "Formula";
                    con =txtFormula ;
                    break;
                default:
                    base.ubtLookUp_Click(sender, e);
                    return;
            }

            if (mblnLookUpSortByDescription) strSql = DataFunctions.GetSqlByDescriptionSort(strSql);
            mblnLookUpSortByDescription = false;
            mdbcon.ExecuteSql("Delete from mUserLookUpFormStartStatus where username='" + SysVar.UserName + "'");
            WP.Modules.frmLookUp frmlookup = new WP.Modules.frmLookUp(mdbcon, strSql, strCaption, strFindValue, strLookUpField);
            frmlookup.ShowDialog();
            if (frmlookup.HaveReturnValue)
            {
                if (con != null)
                {
                    string strBefore = txtFormula.Text.Substring(0, txtFormula.SelectionStart);
                    string strAfter = txtFormula.Text.Substring(txtFormula.SelectionStart, txtFormula.Text.Length - txtFormula.SelectionStart - txtFormula.SelectionLength);
                    
                    string strSelText = frmlookup.GetReturnValue(strLookUpField);
                    txtFormula.Text = strBefore + strSelText + strAfter;
                   
                    if (!blnHasSetFocus)
                    {
                        con.Focus();
                        txtFormula.SelectionStart = strBefore.Length + strSelText.Length;
                        txtFormula.SelectionLength = 0; 
                        blnHasSetFocus = true;
                    }
                }
            }
            frmlookup.Close();
        }

        private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void dgvItem_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex < 0) return;
            if (this.dgvItem.CurrentRow == null) return;
            if (CvtFunc.ToString(this.dgvItem.Rows[e.RowIndex].Cells[(int)DtlCol.CHNName].Value) != "")
            {
                string strSelCHNName = CvtFunc.ToString(this.dgvItem.Rows[e.RowIndex].Cells[(int)DtlCol.CHNName].Value);
                string strBefore = txtFormula.Text.Substring(0, txtFormula.SelectionStart);
                string strAfter = txtFormula.Text.Substring(txtFormula.SelectionStart, txtFormula.Text.Length - txtFormula.SelectionStart - txtFormula.SelectionLength);
                txtFormula.Text = strBefore + strSelCHNName + strAfter;
            }
            txtFormula.SelectionStart = txtFormula.Text.Length;
        }

        private void btnButton_Click(object sender, EventArgs e)
        {
            Button butDig = sender as Button;
            string strSelText = butDig.Text;
            string strBefore = txtFormula.Text.Substring(0, txtFormula.SelectionStart);
            string strAfter = txtFormula.Text.Substring(txtFormula.SelectionStart, txtFormula.Text.Length - txtFormula.SelectionStart - txtFormula.SelectionLength);
            txtFormula.Text = strBefore + strSelText + strAfter;
            
            txtFormula.SelectionStart = txtFormula.Text.Length;
        }

        private void frmRMWKFormula_Load(object sender, EventArgs e)
        {
            
        }

    }
}
